package com.luck.easyfoodbatch.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.entity.Note;
import com.luck.easyfoodbatch.entity.OrderM;
import com.luck.easyfoodbatch.entity.UseYdx;
import com.luck.easyfoodbatch.mvp.cart.ConfirmSingeOrderModel;
import com.luck.easyfoodbatch.mvp.cart.ConfirmSingeOrderPresenter;
import com.luck.easyfoodbatch.mvp.cart.ConfirmSingeOrderView;
import com.luck.easyfoodbatch.mvp.cart.EditOrderAddressModel;
import com.luck.easyfoodbatch.mvp.cart.EditOrderAddressPresenter;
import com.luck.easyfoodbatch.mvp.cart.EditOrderAddressView;
import com.luck.easyfoodbatch.mvp.mine.ChooseVoucherModel;
import com.luck.easyfoodbatch.mvp.mine.ChooseVoucherPresenter;
import com.luck.easyfoodbatch.mvp.mine.ChooseVoucherView;
import com.luck.easyfoodbatch.mvp.mine.MyAddressModel;
import com.luck.easyfoodbatch.mvp.mine.MyAddressPresenter;
import com.luck.easyfoodbatch.mvp.mine.MyAddressView;
import com.luck.easyfoodbatch.mvp.model.ChooseVoucherM;
import com.luck.easyfoodbatch.mvp.model.Collect;
import com.luck.easyfoodbatch.mvp.model.MyAddress;
import com.luck.easyfoodbatch.mvp.model.PlaceOrderByM;
import com.luck.easyfoodbatch.mvp.order.OrderCancelYdxModel;
import com.luck.easyfoodbatch.mvp.order.OrderCancelYdxPresenter;
import com.luck.easyfoodbatch.mvp.order.OrderCancelYdxView;
import com.luck.easyfoodbatch.mvp.order.OrderUseYdxModel;
import com.luck.easyfoodbatch.mvp.order.OrderUseYdxPresenter;
import com.luck.easyfoodbatch.mvp.order.OrderUseYdxView;
import com.luck.easyfoodbatch.pay.PayResult;
import com.luck.easyfoodbatch.simcpux.Constants;
import com.luck.easyfoodbatch.util.AlipayUtil;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConfirmOrderActivity extends BaseActivity implements MyAddressView, ChooseVoucherView, OrderUseYdxView, OrderCancelYdxView, EditOrderAddressView, ConfirmSingeOrderView {
    @BindView(R.id.tv_next)
    TextView tvNext;
    @BindView(R.id.tv_sum_money)
    TextView tv_sum_money;
    @BindView(R.id.tv_order_num)
    TextView tv_order_num;
    @BindView(R.id.rl_address)
    RelativeLayout rl_address;
    @BindView(R.id.ll_address)
    LinearLayout ll_address;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_address_details)
    TextView tv_address_details;
    @BindView(R.id.ll_seller)
    LinearLayout ll_seller;
    @BindView(R.id.ll_things)
    LinearLayout ll_things;
    @BindView(R.id.ll_things2)
    LinearLayout ll_things2;
    @BindView(R.id.rl_wechat)
    RelativeLayout rl_wechat;
    @BindView(R.id.rl_apply)
    RelativeLayout rl_apply;
    @BindView(R.id.iv_seller)
    ImageView iv_seller;
    @BindView(R.id.iv_wechat)
    ImageView iv_wechat;
    @BindView(R.id.iv_apply)
    ImageView iv_apply;
    @BindView(R.id.rl_coupon)
    RelativeLayout rlCoupon;
    @BindView(R.id.tv_shopName)
    TextView tv_shopName;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_spec)
    TextView tv_spec;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.iv_banner)
    ImageView iv_banner;
    @BindView(R.id.et_note)
    EditText et_note;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_shop_title)
    TextView tv_shop_title;
    @BindView(R.id.tv_ydx_money)
    TextView tv_ydx_money;
    @BindView(R.id.tv_sub_money)
    TextView tv_sub_money;
    @BindView(R.id.tv_vname)
    TextView tv_vname;

    @BindView(R.id.rl_tab1)
    RelativeLayout rl_tab1;
    @BindView(R.id.rl_tab2)
    RelativeLayout rl_tab2;
    @BindView(R.id.rl_tab3)
    RelativeLayout rl_tab3;

    private String address_id = "", order_id = "", vid = "";
    private IWXAPI api;
    private MyAddressModel addressModel;
    private List<MyAddress.DataBean> list = new ArrayList<>();
    private String gid = "";
    private String batch_id = "", note = "";
    private PlaceOrderByM.DataBean shop;
    private static final int SDK_PAY_FLAG = 1;
    private String price = "0.0";
    private int pay_type = 1;
    private OrderUseYdxModel useYdxModel;
    private OrderCancelYdxModel cancelYdxModel;
    private EditOrderAddressModel editOrderAddressModel;
    private ConfirmSingeOrderModel orderModel;
    private ChooseVoucherModel voucherModel;
    private Gson gson;
    private int number = 0;
    private String p_id = "3";
    private int use_ydx = 0;// 1  不使用运东西
    private Handler MyHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    PayResult payResult = new PayResult((String) msg.obj);

                    // 支付宝返回此次支付结果及加签，建议对支付宝签名信息拿签约时支付宝提供的公钥做验签
                    String resultInfo = payResult.getResult();
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
                    if (TextUtils.equals(resultStatus, "9000")) {
                        Bundle bundle = new Bundle();
                        bundle.putInt("type", 0);
                        bundle.putString("status", "");
                        startAct(MyOrderActivity.class, bundle);
                        sendBroadcast("action.app.cart.refresh");
                        finish();
                    } else {
                        // 判断resultStatus 为非“9000”则代表可能支付失败
                        // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
                        if (TextUtils.equals(resultStatus, "8000")) {
                            Toast.makeText(mContext, "支付结果确认中",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
                            Toast.makeText(mContext, "支付失败",
                                    Toast.LENGTH_SHORT).show();
                        }
                        sendBroadcast("action.app.cart.refresh");
                    }
                    break;
                }

            }
        }
    };


    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("action.finishApplyActivity")) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putString("status", "");
                startAct(MyOrderActivity.class, bundle);
                finish();
            } else if (intent.getAction().equals("action.app.refresh.address")) {
                // 删除没一个地址了
                rl_address.setVisibility(View.VISIBLE);
                ll_address.setVisibility(View.GONE);
                tv_name.setText("");
                tv_phone.setText("");
                address_id = "";
                tv_address_details.setText("");
            }
        }
    };

    private int allow_ydx = 0;
    private int is_free_post = 0;
    private int free_post_num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order, "确认订单", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        api = WXAPIFactory.createWXAPI(this, null);
        api.registerApp(Constants.APP_ID);
        gson = new Gson();
        p_id = (String) SharedPreferencesUtil.getData(ConfirmOrderActivity.this, HttpUtil.P_ID, "3");
        registerReceiver(receiver, "action.finishApplyActivity", "action.app.refresh.address");
        useYdxModel = new OrderUseYdxPresenter(this, mContext);
        cancelYdxModel = new OrderCancelYdxPresenter(this, mContext);
        showProgressDialog(getString(R.string.app_loading));
        gid = getIntent().getStringExtra("gid");
        use_ydx = getIntent().getIntExtra("use_ydx", 0);
        shop = (PlaceOrderByM.DataBean) getIntent().getSerializableExtra("shop");
        if (shop != null) {
            List<PlaceOrderByM.DataBean.OrderInfoBean> orderInfo = shop.getOrderInfo();
            address_id = shop.getAddress_id() + "";
            if (!Tools.isNull(address_id) && !address_id.equals("0")) {
                rl_address.setVisibility(View.GONE);
                ll_address.setVisibility(View.VISIBLE);
                tv_name.setText("收货人：" + shop.getAddress_member());
                tv_phone.setText(shop.getAddress_member());
                String street = shop.getAddress_str();
                tv_address_details.setText("收货地址：" + shop.getAddress_str() + " " + street);
            }

            if (orderInfo != null && orderInfo.size() > 0) {
                PlaceOrderByM.DataBean.OrderInfoBean orderInfoBean = orderInfo.get(0);
                String shopName = shop.getShop_name();
                is_free_post = orderInfoBean.getIs_free_post();//（1不包2包）
                String s = orderInfoBean.getFree_post_num();
                if (!Tools.isNull(s)) {
                    free_post_num = Integer.parseInt(s);
                } else {
                    free_post_num = 0;
                }

                order_id = orderInfoBean.getOrder_id() + "";
                String propertyTags = orderInfoBean.getPropertyTags();
                number = orderInfoBean.getNum();
                String thumb = orderInfoBean.getGoods_thumb();
                batch_id = orderInfoBean.getBatch_id() + "";
                String goods_name = orderInfoBean.getGoods_name();
                price = orderInfoBean.getPrice();
                if (!Tools.isNull(goods_name)) {
                    tv_shop_title.setText(goods_name);
                } else {
                    tv_shop_title.setText("");
                }
                if (!Tools.isNull(shopName)) {
                    tv_shopName.setText(shopName);
                } else {
                    tv_shopName.setText("");
                }
                if (!Tools.isNull(shopName)) {
                    tv_shopName.setText(shopName);
                } else {
                    tv_shopName.setText("");
                }
                tv_num.setText("数量 x " + number);
                tv_money.setText("￥" + price);
                tv_sum_money.setText("￥" + shop.getOrder_price());
                if (!Tools.isNull(propertyTags)) {
                    tv_spec.setText("规格：" + propertyTags);
                } else {
                    tv_spec.setText("规格：");
                }
                String period = orderInfoBean.getDue_date() + "";
                if (!Tools.isNull(period)) {
                    tv_time.setText("保质期 至 " + Tools.SubTime2(period));
                } else {
                    tv_time.setText("保质期 至 1970-01-01");
                }
                if (!Tools.isNull(thumb)) {
                    String path = "";
                    if (thumb.startsWith("http")) {
                        path = thumb;
                    } else {
                        path = HttpUtil.IMG_THUMB_BASE_URL + thumb;
                    }
                    Glide.with(mContext)
                            .load(path)
                            .centerCrop()
                            .placeholder(R.mipmap.loading_4b3)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(iv_banner);
                } else {
                    iv_banner.setImageResource(R.mipmap.loading_4b3);
                }
                allow_ydx = shop.getAllow_ydx();
                //（1不包2包）
                if (is_free_post == 1) {
                    switch (allow_ydx) {
                        case 1:
                            // 不使用运东西
                            ll_things.setVisibility(View.GONE);
                            ll_things2.setVisibility(View.VISIBLE);
                            ll_seller.setBackgroundResource(R.drawable.input_ydx);
                            iv_seller.setImageResource(R.mipmap.select_sel_1x);
                            break;
                        case 2:
                            // 使用
//                            if (number < free_post_num) {
//                                // 如果达到了包邮条件
//                                useYdxModel.requestOrderUseYdx(order_id);
//                            } else {
//                                ll_things.setVisibility(View.GONE);
//                                ll_things2.setVisibility(View.VISIBLE);
//                                ll_seller.setBackgroundResource(R.drawable.input_ydx);
//                                iv_seller.setImageResource(R.mipmap.select_sel_1x);
//                            }
                            useYdxModel.requestOrderUseYdx(order_id);
                            break;
                    }
                } else if (is_free_post == 2) {
                    switch (allow_ydx) {
                        case 1:
                            // 不使用运东西
                            ll_things.setVisibility(View.GONE);
                            ll_things2.setVisibility(View.VISIBLE);
                            ll_seller.setBackgroundResource(R.drawable.input_ydx);
                            iv_seller.setImageResource(R.mipmap.select_sel_1x);
                            break;
                        case 2:
                            // 使用
                            if (number < free_post_num) {
                                // 如果达到了包邮条件
                                useYdxModel.requestOrderUseYdx(order_id);
                            } else {
                                ll_things.setVisibility(View.GONE);
                                ll_things2.setVisibility(View.VISIBLE);
                                ll_seller.setBackgroundResource(R.drawable.input_ydx);
                                iv_seller.setImageResource(R.mipmap.select_sel_1x);
                            }
                            break;
                    }
                }
            }
        }
        addressModel = new MyAddressPresenter(this, mContext);
        addressModel.getMyAddressList("1");
    }


    @OnClick({R.id.rl_tab1, R.id.rl_tab2, R.id.rl_tab3, R.id.rl_address, R.id.ll_address, R.id.rl_coupon, R.id.ll_things, R.id.ll_things2, R.id.ll_seller, R.id.rl_wechat, R.id.rl_apply, R.id.tv_next})
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.rl_address:
                bundle.putInt("type", 2);
                bundle.putInt("address_type", 1);
                intent.setClass(mContext, AddressManagementActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, 200);
                break;
            case R.id.ll_address:
                bundle.putInt("type", 2);
                bundle.putInt("address_type", 1);
                intent.setClass(mContext, AddressManagementActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, 200);
                break;
            case R.id.rl_coupon:
                intent.setClass(mContext, MyCouponActivity.class);
                bundle.putString("order_id", order_id);
                bundle.putInt("type", 1);
                intent.putExtras(bundle);
                startActivityForResult(intent, 300);
                break;
            case R.id.ll_things:
                break;
            case R.id.ll_things2:
                // 未使用  //（1不包2包）
                if (is_free_post == 1) {
                    if (allow_ydx == 1) {
                        T.showToast(mContext, "该商品不能使用运东西~");
                    } else {
//                        if (number < free_post_num) {
//                            // 如果达到了包邮条件
//                            useYdxModel.requestOrderUseYdx(order_id);
//                            ll_things.setVisibility(View.VISIBLE);
//                            ll_things2.setVisibility(View.GONE);
//                            iv_seller.setImageResource(R.mipmap.select_def_1x);
//                            ll_seller.setBackgroundResource(R.drawable.input_search);
//                        } else {
//                            T.showToast(mContext, "该商品不能使用运东西~");
//                        }
                        useYdxModel.requestOrderUseYdx(order_id);
                        ll_things.setVisibility(View.VISIBLE);
                        ll_things2.setVisibility(View.GONE);
                        iv_seller.setImageResource(R.mipmap.select_def_1x);
                        ll_seller.setBackgroundResource(R.drawable.input_search);
                    }
                } else if (is_free_post == 2) {
                    if (allow_ydx == 1) {
                        T.showToast(mContext, "该商品不能使用运东西~");
                    } else {
                        if (number < free_post_num) {
                            // 如果达到了包邮条件
                            useYdxModel.requestOrderUseYdx(order_id);
                            ll_things.setVisibility(View.VISIBLE);
                            ll_things2.setVisibility(View.GONE);
                            iv_seller.setImageResource(R.mipmap.select_def_1x);
                            ll_seller.setBackgroundResource(R.drawable.input_search);
                        } else {
                            T.showToast(mContext, "该商品不能使用运东西~");
                        }
                    }
                }

                break;
            case R.id.ll_seller:
                // 联系卖家
                cancelYdxModel.orderCancelYdx(order_id);
                ll_things.setVisibility(View.GONE);
                ll_things2.setVisibility(View.VISIBLE);
                iv_seller.setImageResource(R.mipmap.select_sel_1x);
                ll_seller.setBackgroundResource(R.drawable.input_ydx);
                break;
            case R.id.rl_wechat:
                pay_type = 2;
                iv_wechat.setImageResource(R.mipmap.select_sel_1x);
                iv_apply.setImageResource(R.mipmap.select_def_1x);
                break;
            case R.id.rl_apply:
                pay_type = 1;
                iv_wechat.setImageResource(R.mipmap.select_def_1x);
                iv_apply.setImageResource(R.mipmap.select_sel_1x);
                break;
            case R.id.tv_next:
                if (Tools.isNull(address_id) || address_id.equals("0")) {
                    intent.setClass(mContext, AddressManagementActivity.class);
                    bundle.putInt("type", 2);
                    bundle.putInt("address_type", 1);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, 200);
                    return;
                }
                note = et_note.getText().toString().trim();
                if (Tools.isNull(note)) {
                    note = "";
                }
                List<Note> notes = new ArrayList<>();
                Note noteM = new Note();
                noteM.setNote(note);
                noteM.setOrder_id(order_id);
                notes.add(noteM);

                String note_json = gson.toJson(notes);
                showProgressDialog(getString(R.string.app_loading));
                orderModel = new ConfirmSingeOrderPresenter(this, mContext);
                orderModel.requestConfirmOrder("", order_id, note_json, "1", pay_type + "");
                break;
            case R.id.rl_tab1:
                bundle.putInt("flag", 1);
                startAct(BaseYdxHttpActivity.class, bundle);
                break;
            case R.id.rl_tab2:
                bundle.putInt("flag", 2);
                startAct(BaseYdxHttpActivity.class, bundle);
                break;
            case R.id.rl_tab3:
                bundle.putInt("flag", 3);
                startAct(BaseYdxHttpActivity.class, bundle);
                break;
        }
    }


    @Override
    public void myAddressSuccess(MyAddress model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                list = model.getData();
                if (list != null && list.size() > 0) {
                    for (MyAddress.DataBean bean : list) {
                        // 取默认地址
                        if (bean.getIs_default() == 1) {
                            if (bean.getProvince().equals(p_id)) {
                                rl_address.setVisibility(View.GONE);
                                ll_address.setVisibility(View.VISIBLE);
                                tv_name.setText("收货人：" + bean.getUname());
                                tv_phone.setText(bean.getMobile());
                                String street = bean.getStreet();
                                address_id = bean.getAddress_id();
                                tv_address_details.setText("收货地址：" + bean.getAddress_str() + " " + street);
                                editOrderAddressModel = new EditOrderAddressPresenter(ConfirmOrderActivity.this, mContext);
                                editOrderAddressModel.requestEditOrderAddress(order_id,address_id);
                                break;
                            }else{
                                    rl_address.setVisibility(View.VISIBLE);
                                    ll_address.setVisibility(View.GONE);
                                    tv_name.setText("");
                                    tv_phone.setText("");
                                    address_id = "";
                                    tv_address_details.setText("");
                                break;
                            }
                        } else {
                            rl_address.setVisibility(View.VISIBLE);
                            ll_address.setVisibility(View.GONE);
                            tv_name.setText("");
                            tv_phone.setText("");
                            address_id = "";
                            tv_address_details.setText("");
                        }
                    }
                }
            }
        }
    }


    @Override
    public void orderUseYdxSuccess(UseYdx model) {
        if(ll_address.getVisibility()==View.GONE){

        }else {
            if (model != null) {
                if (model.getStatus() == HttpUtil.RESULT_OK) {
                    UseYdx.DataBean data = model.getData();
                    if (tv_ydx_money != null) {
                        tv_ydx_money.setText("￥" + data.getTotalPostAmount());
                    }else {
                        tv_ydx_money.setText("￥"+0);
                    }
                    int totalPrivilegeAmount = data.getTotalPrivilegeAmount();
                    if (tv_sub_money != null) {
                        tv_sub_money.setText("(可减" + totalPrivilegeAmount + "元)");
                    }
                    double pay_price = data.getTotal_pay_price();
                    tv_sum_money.setText("￥" + pay_price);
                    tv_order_num.setText("共1件包含运费");
                }
            }
        }
    }

    @Override
    public void orderCancelYdxSuccess(UseYdx model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                UseYdx.DataBean data = model.getData();
                tv_ydx_money.setText("￥" + data.getTotalPostAmount());
                int totalPrivilegeAmount = data.getTotalPrivilegeAmount();
                tv_sub_money.setText("(可减" + totalPrivilegeAmount + "元)");
                double pay_price = data.getTotal_pay_price();
                tv_sum_money.setText("￥" + pay_price);
                tv_order_num.setText("共0件包含运费");
            }
        }
    }

    public void editOrderAddressSuccess(Collect model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                if(ll_things.getVisibility()==View.VISIBLE) {
                    useYdxModel.requestOrderUseYdx(order_id);
                }else {
                    cancelYdxModel.orderCancelYdx(order_id);
                }
            } else {
                if(ll_things.getVisibility()==View.VISIBLE) {
                    useYdxModel.requestOrderUseYdx(order_id);
                }else {
                    cancelYdxModel.orderCancelYdx(order_id);
                }
                // 如果修改失败 返回上次地址
                useYdxModel.requestOrderUseYdx(order_id);
                T.showToast(mContext, model.getMsg());
            }
        }
    }


    @Override
    public void confirmOrderSuccess(OrderM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                // 订单生成成功 支付 1支付宝2微信
                OrderM.DataBean bean = model.getData();
                String params = bean.getParams();
                switch (pay_type) {
                    case 1:
                        new AlipayUtil(ConfirmOrderActivity.this, params, MyHandler);
                        break;
                    case 2:
                        wechatApply(bean);
                        break;
                }

            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    /**
     * 微信支付
     *
     * @param bean
     */
    private void wechatApply(OrderM.DataBean bean) {
        try {
            PayReq req = new PayReq();
            req.appId = bean.getAppid();
            req.partnerId = bean.getPartnerid();
            req.prepayId = bean.getPrepayid();
            req.nonceStr = bean.getNoncestr();
            req.timeStamp = bean.getTimestamp() + "";
            req.packageValue = bean.getPackageX();
            req.sign = bean.getSign();
            Toast.makeText(this, "正在发起微信支付，请稍候...", Toast.LENGTH_SHORT).show();
            // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
            api.registerApp(Constants.APP_ID);
            api.sendReq(req);
        } catch (Exception e) {
            Log.e("PAY_GET", "异常：" + e.getMessage());
        }
    }

    @Override
    public void chooseVoucherSuccess(ChooseVoucherM model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                ChooseVoucherM.DataBean data = model.getData();
                tv_sum_money.setText("￥" + data.getReal_price());
            }
        }
    }

    @Override
    public void httpStatusError() {
    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    if (data != null) {
                        MyAddress.DataBean bean = (MyAddress.DataBean) data.getSerializableExtra("bean");
                        if (bean != null) {
                            rl_address.setVisibility(View.GONE);
                            ll_address.setVisibility(View.VISIBLE);
                            tv_name.setText("收货人：" + bean.getUname());
                            tv_phone.setText(bean.getMobile());
                            address_id = bean.getAddress_id();
                            String street = bean.getStreet();
                            tv_address_details.setText("收货地址：" + bean.getAddress_str() + " " + street);
                            showProgressDialog(getString(R.string.app_loading));
                            editOrderAddressModel = new EditOrderAddressPresenter(ConfirmOrderActivity.this, mContext);
                            editOrderAddressModel.requestEditOrderAddress(order_id, address_id);
                        }
                    }
                    break;
                case 300:
                    if (data != null) {
                        voucherModel = new ChooseVoucherPresenter(ConfirmOrderActivity.this, mContext);
                        String dec_money = data.getStringExtra("money");
                        if (!Tools.isNull(dec_money)) {
                            tv_vname.setText(dec_money + "元");
                            vid = data.getStringExtra("vid");
                        } else {
                            tv_vname.setText("");
                            // 不使用优惠券
                            vid = 0 + "";
                        }
                        voucherModel.requestChooseVoucher(order_id, vid, order_id);
                    }
                    break;
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}
